package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.ZtCouponBo;
import org.dromara.system.domain.vo.ZtCouponVo;
import org.dromara.system.domain.ZtCoupon;
import org.dromara.system.mapper.ZtCouponMapper;
import org.dromara.system.service.IZtCouponService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 优惠卷Service业务层处理
 *
 * @author Lion Li
 * @date 2024-06-25
 */
@RequiredArgsConstructor
@Service
public class ZtCouponServiceImpl implements IZtCouponService {

    private final ZtCouponMapper baseMapper;

    /**
     * 查询优惠卷
     */
    @Override
    public ZtCouponVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询优惠卷列表
     */
    @Override
    public TableDataInfo<ZtCouponVo> queryPageList(ZtCouponBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZtCoupon> lqw = buildQueryWrapper(bo);
        Page<ZtCouponVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询优惠卷列表
     */
    @Override
    public List<ZtCouponVo> queryList(ZtCouponBo bo) {
        LambdaQueryWrapper<ZtCoupon> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZtCoupon> buildQueryWrapper(ZtCouponBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZtCoupon> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ZtCoupon::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ZtCoupon::getName, bo.getName());
        lqw.eq(bo.getExpirationDate() != null, ZtCoupon::getExpirationDate, bo.getExpirationDate());
        lqw.eq(bo.getClaimValidDays() != null, ZtCoupon::getClaimValidDays, bo.getClaimValidDays());
        lqw.eq(bo.getAmountOfMoney() != null, ZtCoupon::getAmountOfMoney, bo.getAmountOfMoney());
        lqw.eq(bo.getMinAmount() != null, ZtCoupon::getMinAmount, bo.getMinAmount());
        return lqw;
    }

    /**
     * 新增优惠卷
     */
    @Override
    public Boolean insertByBo(ZtCouponBo bo) {
        ZtCoupon add = MapstructUtils.convert(bo, ZtCoupon.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改优惠卷
     */
    @Override
    public Boolean updateByBo(ZtCouponBo bo) {
        ZtCoupon update = MapstructUtils.convert(bo, ZtCoupon.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZtCoupon entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除优惠卷
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
